package verins.GUI;

import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.TableModel;
import verins.BO.PadronElectoralBO;
import verins.BO.ProcesoElectoralBO;
import verins.beans.Padronelectoral;
import verins.beans.Procesoelectoral;
import javax.swing.table.DefaultTableModel;
import verins.beans.Usuario;


public class frmProcesarPadrones extends javax.swing.JFrame {


    private Procesoelectoral procesoEle=null;
    private List<Padronelectoral> padrones=null;
    Usuario usuario=null;
    public frmProcesarPadrones(Usuario usuario) {
        initComponents();
        verins.Utils.Comunes.limpiarTabla(tblPadrones);
        verins.Utils.Comunes.centrarVentana(this);
        this.setSize(this.getPreferredSize());
        this.usuario=usuario;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblProcesoElectoral = new javax.swing.JLabel();
        btnBuscar = new javax.swing.JButton();
        scpnPadrones = new javax.swing.JScrollPane();
        tblPadrones = new javax.swing.JTable();
        btnBusq = new javax.swing.JButton();
        txtProcesoElec = new javax.swing.JTextField();
        btnProcesar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        lblProcesoElectoral.setText("Proceso Electoral");

        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        tblPadrones.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Codigo", "Partido", "Estado"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblPadrones.setEnabled(false);
        scpnPadrones.setViewportView(tblPadrones);

        btnBusq.setText("...");
        btnBusq.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBusqActionPerformed(evt);
            }
        });

        txtProcesoElec.setEnabled(false);

        btnProcesar.setText("Procesar Todos");
        btnProcesar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProcesarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnProcesar)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(scpnPadrones, javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(lblProcesoElectoral, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(txtProcesoElec, javax.swing.GroupLayout.PREFERRED_SIZE, 345, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(btnBusq, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGap(10, 10, 10))))
                .addGap(50, 50, 50))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblProcesoElectoral)
                    .addComponent(txtProcesoElec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBusq))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnBuscar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scpnPadrones, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnProcesar)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        if (procesoEle==null){
            verins.Utils.Mensajes.alerta("Seleccione un proceso electoral");
            return;
        }

        padrones = PadronElectoralBO.findByProcesoElectoral(procesoEle.getIdprocesoelectoral());
        verins.Utils.Comunes.limpiarTabla(tblPadrones);
        DefaultTableModel model= (DefaultTableModel)tblPadrones.getModel();
        for (Padronelectoral p:padrones){
            Object[] fila = {
                p.getIdpadronelectoral().toString(),
                p.getIdpartidopolitico().getNombre(),
                p.getEstado().getDescripcion()
            };
            model.addRow(fila);
        }
        verins.Utils.Comunes.setearColumnas(tblPadrones);
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void btnBusqActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBusqActionPerformed
        frmBusquedaProcesoElectoral frm=new frmBusquedaProcesoElectoral(null,this);
        frm.setearBotonesBusqueda();
        frm.setVisible(true);
        procesoEle=frm.proceso;
        if (procesoEle!=null){
            procesoEle=frm.proceso;
            txtProcesoElec.setText(procesoEle.getNombre());
        }
}//GEN-LAST:event_btnBusqActionPerformed

    private void btnProcesarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProcesarActionPerformed
        DefaultTableModel model= (DefaultTableModel)tblPadrones.getModel();
        if (model.getRowCount()<=0){
            verins.Utils.Mensajes.alerta("No hay padrones por procesar para el proceso electoral");
            return;
        }

        for (int i=0; i<model.getRowCount(); i++){
            String codigo=(String) model.getValueAt(i, 0);
            Padronelectoral padron=null;
            for (int j=0; j<padrones.size(); j++){
                if (padrones.get(j).getIdpadronelectoral().equals(codigo)){
                    padron=padrones.get(j);
                    break;
                }
            }
            model.setValueAt("Procesado", i, 2);
            if (padron==null) break;
            PadronElectoralBO.procesarPadron(padron,procesoEle.getProcesoempadronamientoList().get(0),usuario);
        }
        PadronElectoralBO.verificarInscripcion(procesoEle, procesoEle.getProcesoempadronamientoList().get(0));
        JOptionPane.showMessageDialog(null, "Proceso Terminado");
    }//GEN-LAST:event_btnProcesarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnBusq;
    private javax.swing.JButton btnProcesar;
    private javax.swing.JLabel lblProcesoElectoral;
    private javax.swing.JScrollPane scpnPadrones;
    private javax.swing.JTable tblPadrones;
    private javax.swing.JTextField txtProcesoElec;
    // End of variables declaration//GEN-END:variables
   
}
